﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DAL;
using BLL;
using BOL;
using DevComponents.DotNetBar;
using DevComponents.AdvTree;

namespace QuanLyHangXeKhach
{
    public partial class frmCapNhatTuyenDuong : UserControl
    {
        CapNhatTuyenDuong cntd = new CapNhatTuyenDuong();
        CapNhatDiemDung cndd = new CapNhatDiemDung();
        CapNhatNoiDen cnnden = new CapNhatNoiDen();
        CapNhatNoiDi cnnd = new CapNhatNoiDi();

        DieuKhien dk = new DieuKhien();

        public frmCapNhatTuyenDuong()
        {
            InitializeComponent();
        }

        private void frmCapNhatTuyenDuong_Load(object sender, EventArgs e)
        {
            Loadfrm();
        }

        public void loadcbonoidi()
        {
            cobnoidi.DataSource = cnnd.laynoidi();
            cobnoidi.DisplayMember = "ten";
            cobnoidi.ValueMember = "ma";
            cobnoidi.Text = "";
        }

        public void loadcbonoiden()
        {
            cobnoiden.DataSource = cnnden.laynoiden();
            cobnoiden.DisplayMember = "ten";
            cobnoiden.ValueMember = "ma";
            cobnoiden.Text = "";
        }

        public void loadcbodiemdung()
        {
            List<thongtindiemdung> ttdd = cndd.laydiemdung();
            cobdiemdungchan.DataSource = ttdd;
            cobdiemdungchan.DisplayMember = "ten";
            cobdiemdungchan.ValueMember = "ma";
            cobdiemdungchan.Text = "";
        }

        public void Loadfrm()
        {
            List<thongtintuyenduong> tuyen = cntd.laytuyenduong();
            advTree1.Nodes.Clear();
            foreach (var row in tuyen)
            {
                advTree1.Nodes.Add(CreatenoidenRow(row.ma, row.diemdi, row.diemdung, row.diemden));
            }

            loadcbonoidi();
            loadcbodiemdung();
            loadcbonoiden();        

            cobnoidi.WatermarkText = "chọn nới khởi hành";
            cobdiemdungchan.WatermarkText = "chọn 1 điểm dừng chân";
            cobnoiden.WatermarkText = "chọn nơi đến";

            
            
            

        }

        private Node CreatenoidenRow(string ma, string noidi, string diemdung, string noiden)
        {
            Node node = new Node(ma);
            node.Cells.Add(new Cell(noidi));
            node.Cells.Add(new Cell(diemdung));
            node.Cells.Add(new Cell(noiden));
            return node;
        }

        #region Điều khiển đóng/mở form
        private DieuKhien _Commands;
        /// <summary>
        /// Gets or sets the commands associated with the control.
        /// </summary>
        public DieuKhien Commands
        {
            get { return _Commands; }
            set
            {
                if (value != _Commands)
                {
                    DieuKhien oldValue = _Commands;
                    _Commands = value;
                    OnCommandsChanged(oldValue, value);
                }
            }
        }
        /// <summary>
        /// Called when Commands property has changed.
        /// </summary>
        /// <param name="oldValue">Old property value</param>
        /// <param name="newValue">New property value</param>
        protected virtual void OnCommandsChanged(DieuKhien oldValue, DieuKhien newValue)
        {
            if (newValue != null)
            {
                btndong.Command = newValue.dongfrmcntuyenduong;
                btnthemdiemden.Command= newValue.shownoiden;
                btnthemdiemdung.Command = newValue.showdiemdung;
                btnthemnoidi.Command = newValue.shownoidi;
                btncapnhatgiatuyen.Command = newValue.showcngiatuyen;
            }
            else
            {
                btndong.Command = null;
                btnthemdiemden.Command= null;
                btnthemdiemdung.Command = null;
                btnthemnoidi.Command = null;
                btncapnhatgiatuyen.Command = null;
            }
        }

        #endregion

        private void advTree1_AfterNodeSelect(object sender, AdvTreeNodeEventArgs e)
        {
            try
            {
                Node node = e.Node;
                txtma.Text = node.Cells[0].Text;
                cobnoidi.Text = node.Cells[1].Text;
                cobdiemdungchan.Text = node.Cells[2].Text;
                cobnoiden.Text = node.Cells[3].Text;                
            }
            catch (Exception) { }
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            btnsua.Text = "Lưu";
            btnxoa.Text = "Hủy bỏ";
            btnthem.Enabled = false;
            txtma.Text = dk.NextID(cntd.matuyenlonnhat().ToString(), "TD");
            cobdiemdungchan.Text = "";
            cobnoiden.Text = "";
            cobnoidi.Text = "";
            this.advTree1.AfterNodeSelect -= new DevComponents.AdvTree.AdvTreeNodeEventHandler(this.advTree1_AfterNodeSelect);
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            if (cobdiemdungchan.Text == "" || cobnoiden.Text == "" || cobnoidi.Text == "")
                MessageBoxEx.Show("Nhập đủ thông tin trước khi tiếp tục", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
            {
                if (btnsua.Text == "Lưu")
                {
                    cntd.themtuyenduong(txtma.Text.Trim(), cobnoidi.SelectedValue.ToString(), cobdiemdungchan.SelectedValue.ToString(), cobnoiden.SelectedValue.ToString());
                    MessageBoxEx.Show("Lưu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtma.Text = dk.NextID(cntd.matuyenlonnhat().ToString(), "TD");
                    Loadfrm();
                }
                else
                {
                    cntd.suatuyenduong(txtma.Text.Trim(), cobnoidi.SelectedValue.ToString(), cobdiemdungchan.SelectedValue.ToString(), cobnoiden.SelectedValue.ToString());
                    MessageBoxEx.Show("Sửa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Loadfrm();
                }

            }
            
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            if (btnxoa.Text == "Hủy bỏ")
            {
                btnthem.Enabled = true;
                btnsua.Text = "Sửa";
                btnxoa.Text = "Xóa";
                txtma.Text = "";
                cobdiemdungchan.Text = "";
                cobnoiden.Text = "";
                cobnoidi.Text = "";
                this.advTree1.AfterNodeSelect += new DevComponents.AdvTree.AdvTreeNodeEventHandler(this.advTree1_AfterNodeSelect);
            }
            else
            {
                if (txtma.Text == "")
                    MessageBoxEx.Show("Chọn một đối tượng muốn xóa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                {
                    DialogResult dr = MessageBoxEx.Show("Bạn muốn xóa tuyến đường " + txtma.Text + " này ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                    if (dr == DialogResult.Yes)
                    {
                        cntd.xoatuyenduong(txtma.Text.Trim());
                        MessageBoxEx.Show("Xóa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Loadfrm();
                    }
                }
            }
        }

       
    }
}
